package utils

import (
	"github.com/mojocn/base64Captcha"
)

type ConfigType struct {
	Id          string
	VerifyValue string
	// digit chinese math string audio
	CaptchaType   string
	DriverAudio   *base64Captcha.DriverAudio
	DriverString  *base64Captcha.DriverString
	DriverChinese *base64Captcha.DriverChinese
	DriverMath    *base64Captcha.DriverMath
	DriverDigit   *base64Captcha.DriverDigit
}

var store = base64Captcha.DefaultMemStore

func GenerateCaptcha(confg ConfigType) (id string, b64s string, err error) {
	var driver base64Captcha.Driver
	//create base64 encoding captcha
	switch confg.CaptchaType {
	case "digit":
		driver = confg.DriverDigit
	case "string":
		driver = confg.DriverString.ConvertFonts()
	case "math":
		driver = confg.DriverMath.ConvertFonts()
	case "chinese":
		driver = confg.DriverChinese.ConvertFonts()
	case "audio":
		driver = confg.DriverAudio
	default:
		driver = confg.DriverDigit
	}
	c := base64Captcha.NewCaptcha(driver, store)
	return c.Generate()
}

func VerifyCaptcha(params ConfigType) bool {
	return store.Verify(params.Id, params.VerifyValue, true)
}
